package action
{
	import event.BaseEvent;
	import event.IEventNames;
	
	import gojoe.Go_user;
	
	import util.RemoteServiceInvoker;
	import util.UrlParser;
	
	
    [Event(name=IEventNames.LOGIN_EVENT , type="event.BaseEvent")]
	/**
	 * Encapsulates the login process which calls the php service gojoe.login.LoginManager.
	 * When a user was found a IEventNames.LOGIN_EVENT of type BaseEvent will be fired.
	 * The User may be accessed by BaseEvent.data.user 
	 *  
	 *  	 
	 * @author jbaeck
	 * 
	 */
	public class LoginAction extends DefaultAction
	{
		public var defServiceName:String = "tojoe.login.LoginManager";
		public var defServiceMethod:String = "login";
		
		public function LoginAction()
		{
		}
		
		public function login(user:String=null,pwd:String = null,remoteServiceName:String = null,remoteServiceMethod:String = null):void
		{
			var invoker:RemoteServiceInvoker  = RemoteServiceInvoker.getInstance();
			var parser:UrlParser = new UrlParser();
			
			if(user == null)
				user = parser.params.f;
		  	if(pwd == null)
		  		pwd = parser.params.l;
		  	if(remoteServiceName == null)
		  		remoteServiceName = this.defServiceName;
		  	if(remoteServiceMethod == null)
		  		remoteServiceMethod = this.defServiceMethod;
			
			invoker.executeWDM(new Go_user(),remoteServiceName,remoteServiceMethod,loginSucess,loginFault,user,pwd);
			
		}
		
		public function loginSucess(e:*):void
		{
			var evt:BaseEvent = new BaseEvent(IEventNames.LOGIN_EVENT,null,true);
			for each(var user:Go_user in e)
			{
				evt.data.user = user;
				break;
			}
			dispatchEvent(evt);
		}
		
		public function loginFault(e:*):void
		{
			
		}
		
		public function handleResult(e:*):void
		{
		}
	}
}